package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.common.RedisConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/addRedisImgName")
    public Result addRedisImgName(String imgName){
        Jedis resource = jedisPool.getResource();
        resource.sadd(RedisConst.SETMEAL_PIC_RESOURCES,imgName);
        return new Result(true,MessageConst.ADD_IMGNAME_REDIS);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @RequestMapping("/add")
    public Result add(Integer[] ids,@RequestBody Setmeal setmeal){
        setmealService.add(setmeal,ids);
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 获取上传的凭证
     * @return
     */
    @RequestMapping("/getToken")
    public Result getToken(){
        System.out.println("开始生成上传凭证");
        String ak = "zXi4HrIXtsG5bDibApeu4-d3_d2e5_T4O6sbOjfe";
        String sk = "4RhvxWxCCz1UN8B0r8onUpOg0TS8BT8FRUoZYB8-";
        //存储空间的名称
        String bucket = "javamodel-1";
        //创建认证对象
        Auth auth = Auth.create(ak, sk);
        //生成上传凭证
        String token = auth.uploadToken(bucket);
        return new Result(true, MessageConst.GET_QINIU_TOKEN_SUCCESS, token);
    }

//
//    @RequestMapping("/findById")
//    public Result findById(Integer id){
//        CheckGroup checkGroup = checkGroupService.findById(id);
//        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
//    }
//
//    @RequestMapping("/findCheckitemById")
//    public Result findCheckitemById(Integer id){
//        Integer[] ids = checkGroupService.findCheckitemById(id);
//        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,ids);
//    }
//
//    @RequestMapping("/edit")
//    public Result edit(Integer[] ids,@RequestBody CheckGroup checkGroup){
//        checkGroupService.edit(ids,checkGroup);
//        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
//    }
//
//    @RequestMapping("/delById")
//    public Result delById(Integer id){
//        checkGroupService.delById(id);
//        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
//    }
//
//    @RequestMapping("/findAll")
//    public Result findAll(){
//        List all = checkGroupService.findAll();
//        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,all);
//    }

}
